import clr, sys

clr.AddReferenceByPartialName("Microsoft.Office.Interop.Outlook")
clr.AddReferenceByPartialName("office")

from Microsoft.Office.Interop import Outlook
from Microsoft import Office

from System.Reflection import *

from MailHandler import *
from ActiveXObject import *
from PropertyComboBox import *
from Globals import *
from GtdUi import *

class InspectorUi(GtdUi):
    def __init__(self, inspector, parent, attachOnActivate):
        GtdUi.__init__(self, inspector, parent, attachOnActivate)

        self.inspector = inspector

        Outlook.InspectorEvents_10_Event.add_Close(inspector, self.OnClose)

        if attachOnActivate:
            Outlook.InspectorEvents_10_Event.add_Activate(inspector, self.OnActivate)
        else:
            self.Attach()
            
    def AddGtdControls(self):
        item = self.inspector.CurrentItem
        typename = GetComObjectTypeName(item)
        self.AddCommandBarButton("Open Link Item", 0, "Open the related email", self.OnOpenLinkItem)
        if "MailItem" == typename:
            self.AddCommandBarButton("&-Archieve", 0, "Archieve the items", \
                                     lambda ctrl, cancelDefault: self.OnClick(ArchiveMailItem))
            self.AddCommandBarButton("&=Defer", 0, "Will this take me longer than two minutes?", \
                                     lambda ctrl, cancelDefault: self.OnClick(Defer))
            self.AddCommandBarButton("&*Action", 0, "Can I resolve this email in less than two minutes?", \
                                     lambda ctrl, cancelDefault: self.OnClick(NextAction))
        elif "TaskItem" == typename or "AppointmentItem" == typename:
            projectControl = PropertyComboBox(self.gtdBar, "Project", Settings.projects, self.inspector.CurrentItem).GetControl()
            projectControl.TooltipText = "Set related project"

            actionControl = PropertyComboBox(self.gtdBar, "Action", Settings.actions, self.inspector.CurrentItem).GetControl()
            actionControl.TooltipText = "Set next action"
        else:
            pass

    def OnClick(self, func):
        try:
            item = self.inspector.CurrentItem
            if "MailItem" == GetComObjectTypeName(item):
                func(ActiveXObject(item))
        except:
            ReportException()

    def OnOpenLinkItem(self, ctrl, cancelDefault):
        try:
            item = ActiveXObject(self.inspector.CurrentItem)
            gtdProperties = GtdProperties(ActiveXObject(item.getUserProperties()))
            if gtdProperties.getGtd():
                linkFolder = gtdProperties.getGtdLinkFolder()
                id = gtdProperties.getGtdId()
                linkType = gtdProperties.getGtdLinkType()
                linkFolder = FindFolderByFullPath(item.getSession().Folders, linkFolder)
                if linkFolder != None and linkType != None and id != None:
                    linkItem = GetLinkItem(linkFolder, linkType, id)
                    if linkItem != None:
                        linkItem.Display(False)
        except:
            ReportException()
